"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.copyDirectorySync = exports.getPackage = void 0;
const http_1 = __importDefault(require("http"));
const node_assert_1 = __importDefault(require("node:assert"));
const fs_1 = __importDefault(require("fs"));
const fs_extra_1 = require("fs-extra");
function request(path, registry, options = {}) {
    (0, node_assert_1.default)(path, "path must be provided");
    return http_1.default.request(`${registry}/${path}`, options, (res) => {
        let data = "";
        res.on("data", (chunk) => {
            data += chunk;
        });
        console.log(data, "data");
        return data;
    });
}
// function pick(obj = {}, keys: string[] = []) {
//   const ret = {};
//   keys.forEach((key) => {
//     if (key && obj[key]) {
//       ret[key] = obj[key];
//     }
//   });
//   return ret;
// }
function getPackage(packageName, registry, packageVersion) {
    return __awaiter(this, void 0, void 0, function* () {
        const path = packageVersion
            ? `${packageName}/${packageVersion}`
            : packageName;
        const data = yield request(path, registry);
        const info = data;
        console.log("info", info);
        //    packageVersion
        //     ? data
        //     : pick(data, [
        //         "name",
        //         "description",
        //         "dist-tags",
        //         "author",
        //         "repository",
        //         "keywords",
        //         "license",
        //       ]);
        return info;
    });
}
exports.getPackage = getPackage;
/**
 * 复制目录，包含其所有文件和子目录
 * @param src 要复制的源目录路径
 * @param dest 目标路径
 * @return { success: boolean, errMsg: string }
 */
function copyDirectorySync(src, dest) {
    var _a;
    // 不存在目标目录的情况下，直接创建
    if (!fs_1.default.existsSync(dest)) {
        fs_1.default.mkdirSync(dest, { recursive: true });
    }
    // 目录下包含任何 dir 和 file 返回异常
    if ((_a = fs_1.default.readdirSync(dest)) === null || _a === void 0 ? void 0 : _a.length) {
        return {
            success: false,
            errMsg: "当前目录存在其他文件可能被覆盖，请选择一个空目录",
        };
    }
    // 可以直接复制文件或目录，Node 版本必须 >= 16.7.0
    (0, fs_extra_1.copySync)(src, dest);
    return {
        success: true,
        errMsg: "",
    };
}
exports.copyDirectorySync = copyDirectorySync;
